# Extreme weather events do not increase political parties' environmental attention

#'   This script 
#'    > cleans ParlGov Cabinet Data into party*day format

# Tim Wappenhans, António Valentim, Heike Klüver, and Lukas F. Stoetzer
# First: 23-02-2023
# Last: 14-02-2024


# PACKAGES ---------------------------------------------------------------------
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
  tidyverse,
  lubridate,
  stringi,
  here
)


# CLEANING ---------------------------------------------------------------------
# get all countries observed
country_name_list <- read_csv(here("data/data_input/partypress.csv"))  |> 
  distinct(country_name) |> 
  pull()

# filter Parlgov Database accordingly
df.cabinet_party <- read_csv(here("data/data_input/view_cabinet.csv")) |> 
  
  # bring into press release format
  mutate(
    election_date = as.Date(election_date),
    country_name = tolower(country_name),
    parlgov_id = party_id,
    country_name = ifelse(country_name == "united kingdom", 
                               "uk", country_name)
    ) |> 
  
  # filter only relevant time horizon and countries
  filter(
    election_date > as.Date("2000-01-01"),
    country_name %in% country_name_list
    ) 


# GOVT' VS OPPOSITION ----------------------------------------------------------
# get end date of each cabinet 
df.cabinet <- df.cabinet_party |>
  group_by(country_name) |> 
  distinct(country_name, cabinet_id, election_date, start_date) |> 
  mutate(end_date = dplyr::lead(start_date,
                                default = as.Date("2022-01-01")) - days(1)) 



# merge parties and their govt'/opposition status back to cabinets
df.cabinet_party <- df.cabinet_party |> 
  distinct(cabinet_id, parlgov_id, cabinet_party)

df.cabinet <- left_join(df.cabinet, df.cabinet_party,
                         by = "cabinet_id")



# EXPANDING -------------------------------------------------------------------- 
# expand to create party*days
df.cabinet <- df.cabinet |> 
  group_by(cabinet_id, parlgov_id) |> 
  mutate(date = list(seq.Date(start_date, end_date, by = "day")))  |> 
  unnest(cols = c(date)) 



# EXPORT -----------------------------------------------------------------------
write_csv(df.cabinet,
          file = here("data/data_output/cabinet_daily.csv"))